import numpy as np

sex_male = 0
sex_female = 1
sex_divisions_0 = ["男", "女"]
age_divisions_0 = ['<5', '5<=x<15', '15<=x<25', '25<=x<35', '35<=x<45', '45<=x<55', '55<=x<65', '65<=x<75', '75<=x<85',
                   '>=85']
sex_divisions_1 = ["male", "female"]
age_divisions_1 = ['age05', 'age15', 'age25', 'age35', 'age45', 'age55', 'age65', 'age75', 'age85']


def get_max_frequency(data):
    '''参照：https://wap.sciencenet.cn/blog-3428464-1279139.html?mobile=1'''
    mask = np.unique(data)
    tmp = []
    for v in mask:
        tmp.append(np.sum(data == v))
    # ts = np.max(tmp)
    max_v = mask[np.argmax(tmp)]
    # print(f'这个值：{max_v}出现的次数最多，为{ts}次')
    return max_v


class BaseGroups:

    def __init__(self, division):
        self._dic = {}
        self._divisions: list = division

    def clear(self):
        self._dic.clear()

    def add_person(self, _id, opt):
        """
        记录id和分类
        Parameters
        ----------
        _id
        opt  分类

        Returns
        -------

        """
        if _id is None:
            return
        self._dic[_id] = opt

    def _change_key(self, dic):
        keys = [k for k in dic.keys()]

        for k in keys:
            try:
                nk = self._divisions[k]
                dic[nk] = dic.pop(k)
            except:
                print("keys", keys)
                print("k出错了", k)
        return dic

    def get_count_dict(self):
        sexed = {}
        for v in self._dic.values():
            if v in sexed:
                sexed[v] += 1
            else:
                sexed[v] = 1
        self._change_key(sexed)
        return sexed


class BaseGroupsV2(BaseGroups):
    max_detection = 10

    def __init__(self, d0, d1):
        self._dic = {}
        self._d1: list = d0
        self._d2: list = d1

    def add_person(self, _id, opt):
        if _id is None or opt is None:
            return
        if _id in self._dic:
            pass
        else:
            self._dic[_id] = []

        if len(self._dic[_id]) > self.max_detection:  # 只记录前二十个值
            return
        self._dic[_id].append(opt)

    def check_detect_finished(self, _id):
        """检测是否已经完成这个人的检测"""
        if _id in self._dic:
            return len(self._dic[_id]) > self.max_detection  # 只记录前二十个值
        return False

    def set_current_division(self, key_type):
        if key_type == 0:
            self._divisions = self._d1
        else:
            self._divisions = self._d2

    def get_count_dict(self, key_type=0):
        pass
        sexed = {}
        self.set_current_division(key_type)
        for varray in self._dic.values():
            v = get_max_frequency(varray)
            if v in sexed:
                sexed[v] += 1
            else:
                sexed[v] = 1
        self._change_key(sexed)
        return sexed


class SexGroups(BaseGroupsV2):
    def __init__(self):
        super().__init__(sex_divisions_0, sex_divisions_1)


class AgeGroups(BaseGroupsV2):
    def __init__(self):
        super().__init__(age_divisions_0, age_divisions_1)


if __name__ == '__main__':
    a = [1, 2, 3, 45, 5, 5, 5]
    # pgs = AgeGroups()
    # pgs.add_person(1, 1)
    # pgs.add_person(6, 2)
    # pgs.add_person(8, 2)
    # pgs.add_person(87, 3)
    # pgs.add_person(5, 4)
    # print(pgs.get_count_dict())
    #
    # pgs = SexGroups()
    # pgs.add_person(2, sex_male)
    # pgs.add_person(8, sex_female)
    # pgs.add_person(9, sex_female)
    #
    # print(pgs.get_count_dict())
